<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-02-27
 * Time: 13:51
 */
namespace app\api\common;
use think\Controller;
use think\Db;
use think\Exception;

class Index extends Controller
{
    protected static $err_code = 0;

    /**
     *
     * @apiGroup Index
     * @apiVersion 1.0.1
     * @apiDescription 首页店铺+分类
     * @api {post} /index.index 首页店铺+分类
     * @apiName Index_index
     * @apiParam {float} lng 经度
     * @apiParam {float} lat 纬度
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {Object} data.shop_list  附近店铺列表
     * @apiSuccess {int} data.id  店铺id
     * @apiSuccess {int} data.is_self 是否自营（0不是 1是）
     * @apiSuccess {String} data.shop_name 店铺名称
     * @apiSuccess {String} data.shop_keeper 店铺联系人店长
     * @apiSuccess {String} data.phone  店铺联系人电话
     * @apiSuccess {String} data.shop_phone 店铺电话
     * @apiSuccess {String} data.lng 经度
     * @apiSuccess {String} data.lat 纬度
     * @apiSuccess {String} data.address 店铺地址
     * @apiSuccess {int} data.add_time 店铺创建时间
     * @apiSuccess {String} data.logo 店铺logo
     * @apiSuccess {float} data.distance float  距离
     * @apiSuccess {Object} data.category_list 首页分类
     * @apiSuccess {int} data.id 类别id
     * @apiSuccess {String} data.name 类别名称
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
    ...
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function index($param){
        if (!isset($param['lng']) || empty($param['lng']) || !isset($param['lat']) || empty($param['lat'])){
            return ['data' => '', 'code' => 400001, 'msg' => '参数错误'];
        }
         try{
            $info = [];
             $banner_list = self::banner([]);
            if ($banner_list['code'] == 200){
                $info['banner_list'] = $banner_list['data'];
            }else{
                self::$err_code = $banner_list['code'];
                throw new Exception($banner_list['msg']);
            }
            $shop_list = \app\api\common\Shop::nearby_shop($param);
            if($shop_list['code'] == 200){
                if ($shop_list['data']){
                    foreach ($shop_list['data'] as $k=>$v){
                        $shop_list['data'][$k]['logo'] = get_img_url($v['logo']);
                    }
                }
                $info['shop_list'] = $shop_list['data'];
            }else{
                self::$err_code = $shop_list['code'];
                throw new Exception($shop_list['msg']);
            }
             $category_list = \app\api\common\Goods::category();
             if($category_list['code'] == 200){
                 $info['category_list'] = $category_list['data'];
             }else{
                 self::$err_code = $category_list['code'];
                 throw new Exception($category_list['msg']);
             }
             return ['data' =>$info, 'code' => 200, 'msg' => 'success'];
         }catch (Exception $e){
             return ['data' =>'', 'code' => self::$err_code, 'msg' => $e->getMessage()];
         }
    }


     /**
     *
     * @apiGroup Index
     * @apiVersion 1.0.1
     * @apiDescription 首页v2
     * @api {post} /index.index_v2 首页v2
     * @apiName Index_index_v2
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {Object} data.category_list  类目
     * @apiSuccess {int} data.category_list.id  类目id
     * @apiSuccess {String} data.category_list.name  类目名称
     * @apiSuccess {Object} data.banner_one_list 首页顶部轮播
     * @apiSuccess {int} data.banner_one_list.carousel_id  轮播id
     * @apiSuccess {String} data.banner_one_list.title  标题
     * @apiSuccess {int} data.banner_one_list.cate  类型 1店铺 2网址链接 3专题 4单个商品
     * @apiSuccess {String} data.banner_one_list.item_content
     * @apiSuccess {String} data.banner_one_list.couponurl  cate=4 时，才有用，其他类型没用
     * @apiSuccess {String} data.banner_one_list.image_url  封面图
     * @apiSuccess {Object} data.shop_list 附近店铺
     * @apiSuccess {int} data.shop_list.id  店铺id
     * @apiSuccess {int} data.shop_list.is_self  是否自营 0不是 1是
     * @apiSuccess {int} data.shop_list.shop_name  店铺名称
     * @apiSuccess {String} data.shop_list.shop_keeper 店铺联系人
     * @apiSuccess {String} data.shop_list.phone  店铺联系人电话
     * @apiSuccess {String} data.shop_list.shop_phone  店铺电话
     * @apiSuccess {String} data.shop_list.lng  店铺纬度
     * @apiSuccess {String} data.shop_list.lat  店铺经度
     * @apiSuccess {String} data.shop_list.address  店铺地址
     * @apiSuccess {String} data.shop_list.add_time  店铺添加时间
     * @apiSuccess {String} data.shop_list.logo  店铺logo
     * @apiSuccess {int} data.shop_list.distance  距离所在位置 单位米
     * @apiSuccess {Object} data.zz_recommend 甄值推荐
     * @apiSuccess {String} data.zz_recommend.itemid  商品的淘宝/天猫id
     * @apiSuccess {String} data.zz_recommend.itemtitle  商品标题
     * @apiSuccess {String} data.zz_recommend.itemshorttitle  商品短标题
     * @apiSuccess {String} data.zz_recommend.itemprice 在售价
     * @apiSuccess {int} data.zz_recommend.itemsale  月销量
     * @apiSuccess {String} data.zz_recommend.itemendprice  券后价
     * @apiSuccess {String} data.zz_recommend.shoptype  店铺类型 B天猫 C淘宝
     * @apiSuccess {String} data.zz_recommend.couponurl  优惠券链接
     * @apiSuccess {String} data.zz_recommend.itempic  图片
     * @apiSuccess {String} data.zz_recommend.couponmoney  优惠券金额
     * @apiSuccess {String} data.zz_recommend.couponstarttime  优惠券开始时间
     * @apiSuccess {String} data.zz_recommend.couponendtime  优惠券结束时间
     * @apiSuccess {String} data.zz_recommend.share_url  分享链接
     * @apiSuccess {Object} data.banner_two_list 首页中部轮播
     * @apiSuccess {int} data.banner_two_list.carousel_id  轮播id
     * @apiSuccess {String} data.banner_two_list.title  标题
     * @apiSuccess {int} data.banner_two_list.cate  类型 1店铺 2网址链接 3专题 4单个商品
     * @apiSuccess {String} data.banner_two_list.item_content
     * @apiSuccess {String} data.banner_two_list.couponurl  cate=4时，才有用，其他类型没用
     * @apiSuccess {String} data.banner_two_list.image_url  封面图
     * @apiSuccess {Object} data.rq_recommend 人气推荐
     * @apiSuccess {int} data.rq_recommend.total 总条数
     * @apiSuccess {String} data.rq_recommend.per_page 每页条数
     * @apiSuccess {String} data.rq_recommend.current_page 当前页码
     * @apiSuccess {String} data.rq_recommend.last_page 下一页页码
     * @apiSuccess {Object} data.rq_recommend.data 数据列表
     * @apiSuccess {String} data.rq_recommend.data.itemid  商品的淘宝/天猫id
     * @apiSuccess {String} data.rq_recommend.data.itemtitle  商品标题
     * @apiSuccess {String} data.rq_recommend.data.itemshorttitle  商品短标题
     * @apiSuccess {String} data.rq_recommend.data.itemprice 在售价
     * @apiSuccess {int} data.rq_recommend.data.itemsale  月销量
     * @apiSuccess {String} data.rq_recommend.data.itemendprice  券后价
     * @apiSuccess {String} data.rq_recommend.data.shoptype  店铺类型 B天猫 C淘宝
     * @apiSuccess {String} data.rq_recommend.data.couponurl  优惠券链接
     * @apiSuccess {String} data.rq_recommend.data.itempic  图片
     * @apiSuccess {String} data.rq_recommend.data.couponmoney 优惠券金额
     * @apiSuccess {String} data.rq_recommend.data.couponstarttime 优惠券开始时间
     * @apiSuccess {String} data.rq_recommend.data.couponendtime 优惠券结束时间
     * @apiSuccess {String} data.rq_recommend.data.share_url 分享链接
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
    ...
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function index_v2($param){
        if (!isset($param['lng']) || empty($param['lng']) || !isset($param['lat']) || empty($param['lat'])){
            return ['data' => '', 'code' => 400001, 'msg' => '参数错误'];
        }
        try{
            $info = [];
            //分类树
            $category_list = \app\api\common\Goods::category();
            if($category_list['code'] == 200){
                $info['category_list'] = $category_list['data'];
            }else{
                self::$err_code = $category_list['code'];
                throw new Exception($category_list['msg']);
            }
            //首页顶部banner
            $bannerone_list = \app\api\common\Common::banner(['position'=>1]);
            if ($bannerone_list['code'] == 200){
                $info['banner_one_list'] = $bannerone_list['data'];
            }else{
                self::$err_code = $bannerone_list['code'];
                throw new Exception($bannerone_list['msg']);
            }
            //附近店铺
            $shop_list = \app\api\common\Shop::nearby_shop($param);
            if($shop_list['code'] == 200){
                if ($shop_list['data']){
                    foreach ($shop_list['data'] as $k=>$v){
                        $shop_list['data'][$k]['logo'] = get_img_url($v['logo']);
                    }
                }
                $info['shop_list'] = $shop_list['data'];
            }else{
                self::$err_code = $shop_list['code'];
                throw new Exception($shop_list['msg']);
            }
            //甄值推荐
            $info['zz_recommend'] = Db::name('recommend')->field('itemid,itemtitle,itemshorttitle,itemprice,itemsale,itemendprice,shoptype,itempic,couponmoney,couponurl,couponstarttime,couponendtime')->where(['tuijian'=>1])->order('id desc')->limit(3)->select();
            if (!empty($info['zz_recommend'])){
                foreach ($info['zz_recommend'] as $k=>$v){
                    $info['zz_recommend'][$k]['itempic'] = $v['itempic'].'_310x310.jpg';
                    $info['zz_recommend'][$k]['share_url'] = '';
                }
            }
            //首页中部banner
            $bannertwo_list = \app\api\common\Common::banner(['position'=>2]);
            if ($bannertwo_list['code'] == 200){
                $info['banner_two_list'] = $bannertwo_list['data'];
            }else{
                self::$err_code = $bannertwo_list['code'];
                throw new Exception($bannertwo_list['msg']);
            }
            //人气推荐
             $rq_recommend = Db::name('recommend')->field('itemid,itemtitle,itemshorttitle,itemprice,itemsale,itemendprice,shoptype,itempic,couponmoney,couponurl,couponstarttime,couponendtime')->where(['tuijian'=>2])->order('id desc')->paginate('10');
            $info['rq_recommend'] = $rq_recommend->toArray();
            if (!empty($info['rq_recommend']['data'])){
                foreach ($info['rq_recommend']['data'] as $key=>$val){
                    $info['rq_recommend']['data'][$key]['itempic'] = $val['itempic'].'_310x310.jpg';
                    $info['rq_recommend']['data'][$key]['share_url'] = '';
                }
            }
            return ['data' =>$info, 'code' => 200, 'msg' => 'success'];
        }catch (Exception $e){
            return ['data' =>'', 'code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

    /**
     *
     * @apiGroup Index
     * @apiVersion 1.0.2
     * @apiDescription 首页分类商品
     * @api {post} /index.goods 首页分类商品
     * @apiName Index_goods
     * @apiParam {int} cat_id 类目id
     * @apiParam {String} cat_name 类目名称（二级类目才需要传，一级不用传）
     * @apiParam {int} page 默认为1（这个需要在data外）
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.total 总数
     * @apiSuccess {String} data.per_page 每页数量
     * @apiSuccess {int} data.current_page 当前页
     * @apiSuccess {int} data.last_page 最后的页码
     * @apiSuccess {Object} data.data  商品数据
     * @apiSuccess {String} data.data.itemid 淘宝id
     * @apiSuccess {String} data.data.itemtitle 商品标题
     * @apiSuccess {String} data.data.itemprice 商品现价
     * @apiSuccess {String} data.data.itemendprice 商品券后价
     * @apiSuccess {String} data.data.itempic 商品图片
     * @apiSuccess {String} data.data.couponurl 优惠券链接
     * @apiSuccess {String} data.data.shoptype 店铺类型：天猫（B）C店（C）
     * @apiSuccess {int} data.data.itemsale 销量
     * @apiSuccess {String} data.data.couponmoney 优惠券金额
     * @apiSuccess {String} data.data.couponstarttime 优惠券开始时间
     * @apiSuccess {String} data.data.couponendtime 优惠券结束时间
     * @apiSuccess {String} data.data.share_url 分享链接
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
    ...
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function goods($param){
        if (!isset($param['cat_id']) || empty($param['cat_id'])){
            return ['data' => '', 'code' => 400001, 'msg' => '参数错误'];
        }
        try{
            $cat_name = db('items_cate')->where(['id'=>$param['cat_id'],'status'=>1])->value('name');
            if (empty($cat_name)){
                self::$err_code = 400030;
                throw new Exception(config('error_code.'.self::$err_code));
            }
            if (!empty($param['cat_name'])){
                $where['itemtitle'] = ['like',"%".trim($param['cat_name'])."%"];
            }
            $where['fqcat'] = ['eq',$param['cat_id']];
            $where['couponstarttime'] = ['lt',time()];
            $where['couponendtime'] = ['gt',time()];
            $where['start_time'] = ['lt',time()];
            $where['end_time'] = ['gt',time()];
            $goods_list = Db::table('yf_items')
                ->field('itemid,itemtitle,itemprice,itemendprice,itempic,couponurl,shoptype,itemsale,couponmoney,couponstarttime,couponendtime')
                ->where($where)
                ->order('couponstarttime desc')
                ->paginate('10');
            $goods_list = $goods_list->toArray();
            if (!empty($goods_list['data'])){
                foreach ($goods_list['data'] as $k=>$v){
                    $goods_list['data'][$k]['share_url'] = '';
                }
            }
            return ['data' =>$goods_list, 'code' => 200, 'msg' => 'success'];
        }catch (Exception $e){
            return ['data' =>'', 'code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

     /**
     *
     * @apiGroup Index
     * @apiVersion 1.0.1
     * @apiDescription 首页轮播
     * @api {post} /index.banner 首页轮播
     * @apiName Index_banner
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.cate  类型 1:店铺 2:网址链接
     * @apiSuccess {String} data.item_content  内容 cate为1则为店铺id， cate为2则为网址链接
     * @apiSuccess {int} data.image_url  图片链接
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
    ...
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function banner($param){
        try{
            $banner_list = db('carousel')->field('cate,item_content,image image_url')->where(['status'=>1,'position'=>1,'start_time'=>['lt',time()],'end_time'=>['gt',time()]])->order('sort asc')->select();
            if ($banner_list){
                foreach ($banner_list as $k=>$v){
                    $banner_list[$k]['image_url'] = get_img_url($v['image_url']);
                }
            }else{
                $banner_list = [];
            }
            return ['data' =>$banner_list, 'code' => 200, 'msg' => 'success'];
        }catch (Exception $e){
            return ['data' =>'', 'code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

     /**
     *
     * @apiGroup Index
     * @apiVersion 1.0.1
     * @apiDescription 推荐列表
     * @api {post} /index.recommend 推荐列表
     * @apiName Index_recommend
     * @apiParam {int} recommend_cate 推荐类型 1甄值推荐 2人气推荐
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.total 总条数
     * @apiSuccess {String} data.per_page 每页条数
     * @apiSuccess {String} data.current_page 当前页码
     * @apiSuccess {String} data.last_page 下一页页码
     * @apiSuccess {Object} data.data 数据列表
     * @apiSuccess {String} data.data.itemid  商品的淘宝/天猫id
     * @apiSuccess {String} data.data.itemtitle  商品标题
     * @apiSuccess {String} data.data.itemshorttitle  商品短标题
     * @apiSuccess {String} data.data.itemprice 在售价
     * @apiSuccess {int} data.data.itemsale  月销量
     * @apiSuccess {String} data.data.itemendprice  券后价
     * @apiSuccess {String} data.data.shoptype  店铺类型 B天猫 C淘宝
     * @apiSuccess {String} data.data.couponurl  优惠券链接
     * @apiSuccess {String} data.data.itempic  图片
     * @apiSuccess {String} data.data.couponmoney  优惠券金额
     * @apiSuccess {String} data.data.couponstarttime  优惠券开始时间
     * @apiSuccess {String} data.data.couponendtime  优惠券结束时间
     * @apiSuccess {String} data.data.share_url  分享链接
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
    ...
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function recommend($param){
        if (!isset($param['recommend_cate']) || empty($param['recommend_cate'])){
            return ['data' => '', 'code' => 400001, 'msg' => '参数错误,类型未传'];
        }
        try{
            $items_list = Db::name('recommend')->field('itemid,itemtitle,itemshorttitle,itemprice,itemsale,itemendprice,couponmoney,itempic,shoptype,couponurl,couponstarttime,couponendtime')
                ->where(['tuijian'=>$param['recommend_cate']])
                ->order('id desc')
                ->paginate('10');
            $items_list = $items_list->toArray();
            if (!empty($items_list['data'])){
                foreach ($items_list['data'] as $k=>$v){
                    $item_list['data'][$k]['share_url'] = '';
                }
            }
            return ['data' =>$items_list, 'code' => 200, 'msg' => 'success'];
        }catch (Exception $e){
            return ['data' =>'', 'code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

}